package com.yoomiito.app.ui.order.sureorder

import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import cn.droidlover.xdroidmvp.router.Router
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.copyClipboard
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.ui.order.list.OrderActivity
import com.yoomiito.app.utils.*
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.dialog.ShareSendMoreAdDialog
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.act_buy_result.*
import org.greenrobot.eventbus.EventBus
import java.util.concurrent.TimeUnit

/**
 * 购买成功
 */
class BuyResultActivity : BaseActivity<BuySuccessPresenter>() {
    private var mEventMessage: EventMessage? = null
    private val mAdapter = JiaMaiCodeShowAdapter(null)

    companion object {
        fun start(context: Context, eventMessage: EventMessage) {
            val intent = Intent(context, BuyResultActivity::class.java)
            intent.putExtra(Constant.DATA, eventMessage)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_buy_result

    override fun newP(): BuySuccessPresenter = BuySuccessPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
//        super.initData(savedInstanceState)
        mEventMessage = intent.getParcelableExtra(Constant.DATA)
        back.setOnClickListener {
            finish()
        }

        rcy.layoutManager = LinearLayoutManager(this)
        rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(10f), false))
        rcy.adapter = mAdapter
        val headerView = LayoutInflater.from(this).inflate(R.layout.item_buy_result, null, false)
        mAdapter.addHeaderView(headerView)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            val code = mAdapter.data[position]
            code.copyClipboard(this,"复制成功")
        }
        val mResultIv = headerView.findViewById<ImageView>(R.id.buyResultIv)
        val mResultTv = headerView.findViewById<TextView>(R.id.buyResultTv)
        val toGoodsTv = headerView.findViewById<TextView>(R.id.toGoodsTv)
        val lookOrderTv = headerView.findViewById<TextView>(R.id.lookOrderTv)
        val send = headerView.findViewById<TextView>(R.id.send)
        send.background = ColorUtils.background(R.color.color_FF035B,23f)
        send.visibility = if (Type.GoodsType.GiveMore==mEventMessage?.goodsType&&mEventMessage?.code==Type.EventMsgType.Success) View.VISIBLE else View.GONE
        send.setOnClickListener {
            showLoading()
            p.getSddShareMsg(5)
        }
        toGoodsTv.setOnClickListener {
            finish()
        }
        lookOrderTv.setOnClickListener {
            Router.newIntent(this)
                    .to(OrderActivity::class.java)
                    .putInt(Constant.TYPE, 1)
                    .launch()
            finish()
        }
        if (AllCode.Fail == mEventMessage?.code) {
            buyResultTitle.text = "购买失败"
            mResultIv.setImageResource(R.drawable.order_fail)
            mResultTv.text = "购买失败"
            mResultTv.setTextColor(ResUtil.getColor(R.color.color_ff5100))
            lookOrderTv.visibility = View.GONE
            if (mEventMessage?.goodsType == AllCode.College) {
                toGoodsTv.text = "返回课程"
            }
        }else{
            if (mEventMessage?.goodsType==Type.GoodsType.BaoPin){
                showLoading()
                p.getJiaMiaCode(mEventMessage?.msg!!)
            }
            if (Type.GoodsType.GiveMore==mEventMessage?.goodsType){
                showLoading()
            }
        }
        showLoading()
        Observable.timer(1000,TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe{
                    updateUserInfo()
                }

    }

    fun showData(data:List<String>){
        mAdapter.setNewData(data)
    }

    private fun postMsg(){
        mEventMessage?.eventType = AllCode.BuyComplete
        EventBus.getDefault().post(mEventMessage)
    }

    fun sddShareMsg(shareMsg: ShareMsg?) {
        val dialog = ShareSendMoreAdDialog(context, null, shareMsg!!, ShareSendMoreAdDialog.GoodsListType)
        dialog.setOnShowListener { v: DialogInterface? -> dismissLoading() }
        dialog.show()
    }

    override fun eventMessage(eventMessage: EventMessage?) {
        super.eventMessage(eventMessage)
        if (Type.EventMsgType.UPDATE_USER_INFO == eventMessage?.eventType){
            postMsg()//通知刷新数据
        }
    }

    //    @OnClick({R.id.act_buy_result_back,R.id.act_bus_success_back_goods,R.id.act_bus_result_look_order,R.id.item_share_wx_btn})
//    @OnClick(R.id.act_buy_result_back, R.id.act_bus_success_back_goods, R.id.act_bus_result_look_order)
//    fun onClick(v: View) {
//        when (v.id) {
//            R.id.act_buy_result_back, R.id.act_bus_success_back_goods -> {
//                val eventMessage = EventMessage()
//                eventMessage.eventType = AllCode.BuyComplete
//                EventBus.getDefault().post(eventMessage)
//                finish()
//            }
//            R.id.act_bus_result_look_order -> {
//                Router.newIntent(this)
//                        .to(OrderActivity::class.java)
//                        .putInt(Constant.TYPE, mEventMessage?.goodsType!!)
//                        .launch()
//                finish()
//            }
//            R.id.item_share_wx_btn -> {
//                AllUtils.copyText(this, "18138440721")
//                showToast("复制成功")
//            }
//        }
//    }


}